package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dicItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.selectForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询明细页面的所有字典目录
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        model.addAttribute("systemDictionaries", systemDictionaries);
        //查询上级明细
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectSuperiordetails();
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);

        return "/systemDictionaryItem/list";
    }
    @RequestMapping("/selectByTypeId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeId(Model model,Long id){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByTypeId(id);
        return systemDictionaryItems;
    }

    /*根据目录中的sn来查询某一个类别下所有的详细，比如business对应的是业务大类*/
    @RequestMapping("/selectByTypeSn")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeSn(String sn){
        List<SystemDictionaryItem> systemDictionaryItems= systemDictionaryItemService.selectByTypeSn(sn);
        return systemDictionaryItems;
    }

    //通过parent_id来查询所有业务小类
    @RequestMapping("/selectByParentId")
    @ResponseBody
    public JsonResult selectByParentId(Long parentId){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByParentId(parentId);
        return new JsonResult(systemDictionaryItems);
    }

}
